import { Navigate, useLocation } from 'react-router-dom'

import { isAuth } from '@/utils/token'

export default function RouteAuth ({ children }) {
  const { pathname } = useLocation()
  if (isAuth()) {
    // 已经登录
    if (pathname === '/login') {
      return <Navigate to='/' replace />
    } else {
      return <>{children}</>
    }
  } else {
    // 未登录
    if (pathname === '/login') {
      return <>{children}</>
    } else {
      return <Navigate to="/login" replace />
    }
  }
}